package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IBankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

/**
 * @Author: Dearsil
 * @Date: 2025/1/13 13:53
 * @Version: 1.0
 * @Description: Hello Description!
 */
@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {
    @Override
    public BankAccountResDTO saveOrUpdate(BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {
        if(ObjectUtils.isNull(bankAccountUpsertReqDTO)){
            throw new ForbiddenOperationException("账户信息为空");
        }

        if(ObjectUtils.isNull(bankAccountUpsertReqDTO.getId())){
            bankAccountUpsertReqDTO.setId(UserContext.currentUserId());
        }
        BankAccount bankAccount = BeanUtils.copyProperties(bankAccountUpsertReqDTO, BankAccount.class);
        this.saveOrUpdate(bankAccount);
        //再次查询
        BankAccount account = this.getById(bankAccount.getId());
        return BeanUtils.copyProperties(account, BankAccountResDTO.class);
    }

    @Override
    public BankAccountResDTO currentUserBankAccount() {
        BankAccount account = this.getById(UserContext.currentUserId());
        if(ObjectUtils.isNull(account)){
            throw new ForbiddenOperationException("账户不存在");
        }
        BankAccountResDTO bankAccountResDTO = BeanUtils.copyProperties(account, BankAccountResDTO.class);
        return bankAccountResDTO;
    }
}
